$(function () {
    $('#searchbox').keypress(function (e) {
        var key = e.which; //e.which是按键的值
        if (key == 13) {
            var q = $(this).val();
            if (q && q != '') {
                window.location.href = '/search/' + q;
            }
        }
    });

    $.ajax({
        url: "/blogCount",
        type: "post",
        success: function (data) {
            blogPage(data.data);
        }
    });

    $.ajax({
        url: "/newBlog",
        type: "post",
        success: function (data) {
            if (data.code === 1000) {
                newBlogList(data.data);
            } else {
                swal("错误");
            }
        }
    });

    $.ajax({
        url: "/hotBlog",
        type: "post",
        success: function (data) {
            if (data.code === 1000) {
                hotBlogList(data.data);
            } else {
                swal("错误");
            }
        }
    });

    $.ajax({
        url: "/catalogue/list",
        type: "get",
        success: function (data) {
            if (data.code === 1000) {
                catalogueList(data.data);
            } else {
                swal("错误");
            }
        }
    });

});

function newBlogList(data) {
    $(data).each(function (index) {
        let datum = data[index];
        // console.info(datum);
        let s = "<li class=\"sidebar-list-item\"><a href='/detail?id=" + datum.id + "' class=\"tag tooltipped tooltipped-n\">" + datum.title +
            "                            </a></li>";

        $(".sidebar-list-items").append(s);
    })
}

function hotBlogList(data) {
    $(data).each(function (index) {
        let datum = data[index];
        let s = "<li class=\"sidebar-list-item\"><a rel=\"tag\" href='/detail?id=" + datum.id + "' class=\"tag tooltipped tooltipped-n\">" + datum.title +
            "                            </a></li>";

        $(".hot-list").append(s);
    })
}

function catalogueList(data) {
    $(data).each(function (index) {
        let datum = data[index];
        let s = "";
        if (index === data.length - 1) {
            s = "<li class=\"sidebar-list-item\" style='list-style: none;margin-right: 20px'><a href='javascript:;' " +
                " onclick='getBlogCountByCatalogue(" + datum.id + ")' class=\"tag tooltipped tooltipped-n\">" + datum.name +
                "                            </a></li>";
        } else {
            s = "<li class=\"sidebar-list-item\" style='float: left;list-style: none;margin-right: 20px'><a href='javascript:;' " +
                " onclick='getBlogCountByCatalogue(" + datum.id + ")' class=\"tag tooltipped tooltipped-n\">" + datum.name +
                "                            </a></li>";

        }

        $(".catalogue-list").append(s);
    })
}

function createBlogItem(msg) {
    $("#div-list").empty();
    let hide = $("#Pagination");
    if (msg.length === 0) {
        hide.hide();
        $("#div-list").css({
            "text-align": "center",
        })
        let img = "<img style='width: 330px;height: 650px;margin-top: -60px' src='/blog/amaze/images/timg.png\'/>\n";
        $("#div-list").append(img);
        return;
    }else {
        $("#div-list").css({
            "text-align": "left",
        })
    }
    hide.show();
    $(msg).each(function (index) {

        // console.info(index);

        let msgElement = msg[index];

        console.info(msgElement);

        let time = msgElement.time;
        let date = new Date(time);
        let fullYear = date.getFullYear();
        let month = date.getMonth() + 1;
        let day = date.getDate();
        let hours = date.getHours();
        let minutes = date.getMinutes();
        let seconds = date.getSeconds();

        if (seconds < 10) {
            seconds = "0" + seconds;
        }
        let date1 = fullYear + "-" + month + "-" + day + "- " + hours + ":" + minutes + ":" + seconds;

        let s = "<h1>" + msgElement.title + "</h1>";
        let t = "<div class=\"topic-header\">\n" +
            "                            <div class=\"blog-flex-center\">\n" +
            "                                <div class=\"blog-flex0\">\n" +
            "                                    <img src=\"/admin/dist/img/13.png\" alt=\"\"\n" +
            "                                         class=\"link avatar avatar-image\">\n" +
            "                                </div>\n" +
            "                                <div class=\"author-lockup blog-flex1\">\n" +
            "                                    <a class=\"link\" href='#'>龙</a>\n" +
            "                                    <span class=\"in\">in</span>\n" +
            "                                    <span class=\"category-name\">\n" +
            "                                        <a class=\"category-name\" rel=\"tag\" onclick='getBlogCountByCatalogue(" + msgElement.catalogue_id + ")' href='javascript:;'>" + msgElement.catalogue_name + "</a>\n" +
            "                                    </span>\n" +
            "                                </div>\n" +
            "                            </div>\n" +
            "                            <span class=\"pull-right time pc_time\">" + date1 + "</span>\n" +
            "                        </div>\n" +
            "                        <h1 class=\"post-index-title\">\n" +
            "                            <a itemtype=\"url\" href='/detail?id=" + msgElement.id + "'>\n" +
            "                                " + msgElement.title + "\n" +
            "                            </a>\n" +
            "                        </h1>\n" +
            "                        <div class=\"post-content-preview article-body\">\n" +
            "                            <a href='/detail?id=" + msgElement.id + "'>\n" +
            "                                <img src='" + msgElement.icon + "' alt=''></a>\n" +
            "                        </div>\n" +
            "                        <div class=\"clearfix topic-footer\">";

        $("#div-list").append("</br>").append(t).append("</br>").append("<hr/>");
    });
    let s = "<a href='/' style=\"padding: 3px; width: max-content; height: 20px; text-align: center; text-decoration: none;\">首页</a>\n" +
        "<a href=\"#\" style=\"padding: 3px; width: max-content; height: 20px; text-align: center; text-decoration: none;\">上一页</a>";
}

function getBlog(page, item) {
    let data = {limit: item, offset: page};
    $.ajax({
        url: "/search",
        type: "post",
        contentType: "application/json",
        data: JSON.stringify(data),
        success: function (data) {
            if (data.code === 1000) {
                // console.info(data)
                createBlogItem(data.data);
            } else {
                swal("服务器异常");
            }
        }
    });
}

function blogPage(total) {

    // console.info(total)
    let item_page = 3;
    $("#Pagination").pagination(total, {
        num_edge_entries: 3,        //省略号后面的页数
        num_display_entries: 4,     //省略号前面的页数
        current_page: 0,
        callback: function (page) {
            console.log("page:" + page);
            getBlog(page, item_page);
        },
        items_per_page: item_page,           //每页显示的条目
        prev_text: '上一页',
        next_text: '下一页',
        prev_show_always: true,
        next_show_always: true
    });
}

function search(index, limit) {
    let q = $('#searchbox').val();
    // if (q && q !== '') {
    let json = {limit: limit, offset: index, keyword: q};
    $.ajax({
        url: "/getBlogByKeyword",
        type: "post",
        async: false,
        contentType: "application/json",
        data: JSON.stringify(json),
        success: function (data) {
            keyWordTotal = data.data.total;
            createBlogItem(data.data.blogs);
        }
    })
    // }
}

let keyWordTotal = 0;

function blogPage1() {
    let item_page = 3;
    // search(1, item_page);
    $("#Pagination").pagination(keyWordTotal, {
        num_edge_entries: 3,        //省略号后面的页数
        num_display_entries: 4,     //省略号前面的页数
        current_page: 0,
        callback: function (page) {
            console.log("=========" + page);
            search(page, item_page);
        },
        items_per_page: item_page,           //每页显示的条目
        prev_text: '上一页',
        next_text: '下一页',
        prev_show_always: true,
        next_show_always: true
    });
}

function getBlogCountByCatalogue(catalogueId) {
    console.info(catalogueId);
    let count;
    $.ajax({
        async: false,
        url: "/gbcbc",
        type: "get",
        contentType: "application/json",
        data: {
            catalogueId: catalogueId
        },
        success: function (data) {
            if (data.code === 1000) {
                count = data.data;
            } else {
                swal("服务器异常")
            }
        }
    });
    // getBlogByCatalogue(0, 3, catalogueId);
    $("#Pagination").pagination(count, {
        num_edge_entries: 3,        //省略号后面的页数
        num_display_entries: 4,     //省略号前面的页数
        current_page: 0,
        callback: function (page) {
            console.log("=========" + page);
            getBlogByCatalogue(page, 3, catalogueId);
        },
        items_per_page: 3,           //每页显示的条目
        prev_text: '上一页',
        next_text: '下一页',
        prev_show_always: true,
        next_show_always: true
    });
}

function getBlogByCatalogue(index, limit, catalogueId) {
    $.ajax({
        url: "/gbbc",
        type: "get",
        contentType: "application/json",
        data: {
            pageSize: limit,
            pageNum: index,
            catalogueId: catalogueId
        },
        success: function (data) {
            createBlogItem(data.data);
        }
    })
}